package com.listeningframework.boot.autoconfigure.mail.senders;

import com.listeningframework.boot.autoconfigure.mail.domains.ListeningMailMessage;
import com.listeningframework.boot.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.util.Assert;

/**
 * Listening Mail Sender
 *
 * @author lixining
 * @version $Id: ListeningMailSender.java, v 0.1 2015年11月4日 上午11:14:10 lixining Exp $
 */
public class ListeningSystemMailSender extends ListeningBaseMailSender {
    /**
     * Mail Properties
     */
    @Autowired
    private MailProperties springBootMailProperties;
    /**
     * Java Mail
     */
    @Autowired
    private JavaMailSender springBootMailSender;

    /**
     * Send Simple Mail Message
     *
     * @param mailMessage 邮件内容
     * @throws BusinessException 发送异常
     */
    public void sendSimpleMessage(ListeningMailMessage mailMessage) throws BusinessException {
        Assert.notNull(springBootMailProperties, "mail properties is null");
        try {
            mailMessage.setSendFrom(springBootMailProperties.getUsername());
            springBootMailSender.send(getSimpleMailMessage(mailMessage));
        } catch (Exception e) {
            throw new BusinessException("SimpleMailMessage Send Fail", e);
        }
    }

    /**
     * Send Mime Mail Message
     *
     * @param mailMessage 邮件内容
     * @throws BusinessException 发送异常
     */
    public void sendMimeMessage(ListeningMailMessage mailMessage) throws BusinessException {
        Assert.notNull(springBootMailProperties, "mail properties is null");
        try {
            mailMessage.setSendFrom(springBootMailProperties.getUsername());
            springBootMailSender.send(getMimeMailMessage(springBootMailSender, mailMessage));
        } catch (Exception e) {
            throw new BusinessException("MimeMailMessage Send Fail", e);
        }
    }

}
